﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;

namespace VietKinsNutrition.ThucPham
{
    public partial class FrmTblDm_Nhomthucpham_Thucan : DevExpress.XtraEditors.XtraForm
    {
        ClsTblDm_Nhomthucpham_ThucanBLL ClsTblDm_Nhomthucpham_Thucanbll = new ClsTblDm_Nhomthucpham_ThucanBLL();
        int iBtn = 1;
        
        public FrmTblDm_Nhomthucpham_Thucan()
        {
            InitializeComponent();
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if (iBtn == 1)
            {
                try
                {
                    if (ClsTblDm_Nhomthucpham_Thucanbll.CheckNullTextbox(this.textEdit1) == false || ClsTblDm_Nhomthucpham_Thucanbll.FindSameName(1, this.textEdit1.Text) == false)
                    {
                        return;
                    }
                    if (XtraMessageBox.Show("Bạn có muốn thêm mới?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    { iBtn = 1; textEdit1.Text = ""; return; }
                    this.ClsTblDm_Nhomthucpham_Thucanbll.Insert(this.textEdit1.Text);
                    ClsTblDm_Nhomthucpham_Thucanbll.BingSource_DataGrid(this.gridControl1);
                    textEdit1.Text = "";
                }
                catch { }
            }
            else if (iBtn == 2)
            {

                try
                {
                    if (ClsTblDm_Nhomthucpham_Thucanbll.CheckNullTextbox(this.textEdit1) == false || ClsTblDm_Nhomthucpham_Thucanbll.FindSameName(2, this.textEdit1.Text) == false)
                    {
                        return;
                    }
                    if (XtraMessageBox.Show("Bạn có muốn sửa dữ liệu?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    { iBtn = 1; textEdit1.Text = ""; return; }

                    this.ClsTblDm_Nhomthucpham_Thucanbll.Edit(int.Parse(this.gridView1.GetFocusedRowCellValue("Id").ToString().Trim()), textEdit1.Text);
                    ClsTblDm_Nhomthucpham_Thucanbll.BingSource_DataGrid(this.gridControl1);
                    textEdit1.Text = "";
                    iBtn = 1;
                }
                catch { }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount == 0)
                return;
            ClsTblDm_ThanhphandingduongcuathucanBLL ClsTblDm_Thanhphandingduongcuathucanbll = new ClsTblDm_ThanhphandingduongcuathucanBLL();

            if (ClsTblDm_Thanhphandingduongcuathucanbll.Thanhphandinhduong_ByNhomId_Tenthucan(int.Parse(gridView1.GetFocusedRowCellValue("Id").ToString().Trim())).Rows.Count > 0)
            {
                XtraMessageBox.Show("Đang có dữ liệu, hệ thống không cho phép xóa");
            }
            else
            {
                if (XtraMessageBox.Show("Bạn muốn chắc chắn xóa dữ liệu?", "Chú ý", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        this.ClsTblDm_Nhomthucpham_Thucanbll.Id = int.Parse(gridView1.GetFocusedRowCellValue("Id").ToString().Trim());
                        this.ClsTblDm_Nhomthucpham_Thucanbll.Del();
                        ClsTblDm_Nhomthucpham_Thucanbll.BingSource_DataGrid(this.gridControl1);
                        XtraMessageBox.Show("Xóa dữ liệu thành công");
                        { iBtn = 1; textEdit1.Text = ""; return; }
                    }
                    catch
                    {
                        XtraMessageBox.Show("Xóa thất bại");
                    }
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            iBtn = 2;
            textEdit1.EditValue = gridView1.GetFocusedRowCellValue("Tennhom").ToString();
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            iBtn = 1;
            textEdit1.EditValue = "";
        }

        private void FrmTblDm_Nhomthucpham_Thucan_Load(object sender, EventArgs e)
        {
            this.ClsTblDm_Nhomthucpham_Thucanbll.BingSource_DataGrid(this.gridControl1);
        }
    }
}